//
//  PersonalHeaderView.m
//  lhpc_manager
//
//  Created by 赵永杰 on 2017/9/6.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "PersonalHeaderView.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface PersonalHeaderView ()

@property (nonatomic, strong) UIImageView *iconImage;

@property (nonatomic, strong) UILabel *nameLabel;

@property (nonatomic, strong) UIImageView *sexImage;

@property (nonatomic, strong) UILabel *signLabel;


@end

@implementation PersonalHeaderView

#pragma mark - LifeCycle


 - (instancetype)initWithFrame:(CGRect)frame {
 self = [super initWithFrame:frame];
 if (self) {
 [self setupChildViews];
 }
 return self;
 }

- (void)updateUserInfo:(NSDictionary *)userInfo {
    NSString *iconStr = [userInfo valueForKey:@"userAvatar"];
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:iconStr] placeholderImage:[UIImage imageNamed:@"userAvatar"]];
    
    self.nameLabel.text = [userInfo valueForKey:@"userName"];
    
    NSString *sign = [userInfo valueForKey:@"signature"];
    self.signLabel.text = HLSTR_Is_Empty(sign) ? @"您还没有个性签名~" : sign;
    
    NSInteger sex = [[userInfo valueForKey:@"sex"] integerValue];
    
    NSString *sexStr = sex == 1 ? @"v2_icon_my_male"  : @"v2_icon_my_female";
    
    self.sexImage.image = [UIImage imageNamed:sexStr];
    
}


#pragma mark - Private

- (void)setupChildViews {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.iconImage];
    [self addSubview:self.nameLabel];
    [self addSubview:self.sexImage];
    [self addSubview:self.signLabel];
    [self setNeedsUpdateConstraints];
}

- (void)updateConstraints {
    [super updateConstraints];
    
    [self.iconImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(25);
        make.centerX.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    [self.nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.iconImage.mas_bottom).with.offset(10);
    }];
    
    [self.sexImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.nameLabel);
        make.left.equalTo(self.nameLabel.mas_right).with.offset(8);
    }];
    
    [self.signLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.nameLabel.mas_bottom).with.offset(10);
    }];
    
}

#pragma mark - Action

#pragma mark - Getter

- (UIImageView *)iconImage {
    if (!_iconImage) {
        _iconImage = [[UIImageView alloc] init];
        _iconImage.layer.cornerRadius = 25;
        _iconImage.layer.masksToBounds = YES;
        _iconImage.image = [UIImage imageNamed:@"v2_icon_my_head_male"];
    }
    return _iconImage;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.text = @"lhpc";
        _nameLabel.textColor = BLACKCOLOR;
        _nameLabel.font = [UIFont systemFontOfSize:16];
    }
    return _nameLabel;
}

- (UIImageView *)sexImage {
    if (!_sexImage) {
        _sexImage = [[UIImageView alloc] init];
        _sexImage.image = [UIImage imageNamed:@"v2_icon_my_male"];
    }
    return _sexImage;
}

- (UILabel *)signLabel {
    if (!_signLabel) {
        _signLabel = [[UILabel alloc] init];
        _signLabel.text = @"您还没有个性签名~";
        _signLabel.textColor = GRAYCOLOR;
        _signLabel.font = [UIFont systemFontOfSize:13];
        
    }
    return _signLabel;
}

#pragma mark - Setter

@end
